package cn.yunhe.shiro_demo.config;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public Realm realm() {
        return new Realm();
    }

    @Bean
    public SecurityManager securityManager(Realm realm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();

        shiroFilter.setSecurityManager(securityManager);

        //拦截器
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/login", "anon");
        map.put("/login.html", "anon");
        map.put("/show.html", "authc");
        map.put("/update", "authc,perms[book:update]");
        map.put("/update.html", "authc,perms[book:update]");
        map.put("/add", "authc,perms[book:query]");
        map.put("/add.html", "authc,perms[book:query]");

        shiroFilter.setLoginUrl("/login.html");
        shiroFilter.setUnauthorizedUrl("/unauthorized.html");
        shiroFilter.setFilterChainDefinitionMap(map);
        return shiroFilter;
    }




}
